package xiangya.bysj.client.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import xiangya.bysj.entity.Doctor;
import xiangya.bysj.online.Keeper;
import xiangya.bysj.online.OnlineDoctorService;
import xiangya.bysj.service.impl.DoctorServiceImpl;
import xiangya.bysj.service.impl.SMSServiceImpl;
import xiangya.bysj.util.JsonUtil;
import xiangya.bysj.util.PropertyUtil;
import xiangya.bysj.util.StringUtil;

/**
 * 药师有关操作的controller
 * 
 * @author Mr.zhang
 *
 */
@Controller
@RequestMapping("/client/doctor")
public class DoctorClientController {

	private final static String FLODER = "diplomaFloder";

	/**
	 * response 对象，写json的时候用到
	 */
	@Autowired
	private HttpServletResponse response;
	/**
	 * request对象，文件上传需要
	 */
	@Autowired
	private HttpServletRequest request;

	/**
	 * doctorService 对象，药师有关操作用到
	 */
	@Resource(name = "doctorService")
	private DoctorServiceImpl doctorService;
	/**
	 * SMS 的业务对象
	 */
	@Resource(name = "SMSService")
	private SMSServiceImpl SMSService;

	@Resource
	private OnlineDoctorService onlineDoctorService;

	/**
	 * 注册方法
	 */
	@RequestMapping("/register")
	@ResponseBody
	public String register(String reqJson, String vercode) {
		String result = null;
		Doctor doctor = (Doctor) JsonUtil.getObject(reqJson, Doctor.class);
		try {
			// 验证验证码
			if (true || SMSService.checkVerCode(doctor.getDoctorPhone(), vercode).equals(StringUtil.RES_SUCCESS)) {
				// 在自己的服务器上面注册,同时在百川上面注册
				doctorService.save(doctor);
				// 返回成功结果
				result = StringUtil.RES_SUCCESS;
			} else {
				result = StringUtil.RES_CKVERCODE_FAIL;
			}
		} catch (Exception e) {
			result = StringUtil.RES_FAIL;
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 登陆方法
	 */
	@RequestMapping("/login")
	@ResponseBody
	public Doctor login(String username, String password) {
		Doctor doctor = null;
		try {
			// 登陆成功则返回该用户的所有信息，登陆失败则返回null
			doctor = doctorService.login(username, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return doctor;
	}

	@RequestMapping("/getsome")
	@ResponseBody
	public void getSome() {
		String doctorName = "zhang";
		List<Doctor> list = null;
		try {
			// 根据药师姓名模糊查询，返回结果集
			list = doctorService.getSome(doctorName, -1);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 写入json，反馈给客户端
			JsonUtil.writeJsonByJackson(response, list);
		}
	}

	@RequestMapping("/diploma")
	@ResponseBody
	public String uploadDiploma(@RequestParam(value = "file", required = false) MultipartFile file) {
		String result = "";
		try {
			String path = request.getSession().getServletContext()
					.getRealPath(PropertyUtil.getProperties().getProperty(FLODER));
			String doc = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
			file.getOriginalFilename();
			if (StringUtil.filterFileType(doc)) {
				String destFileName = StringUtil.getFileName(doc);
				File destFile = new File(path, destFileName);
				if (!destFile.exists()) {
					destFile.mkdirs();
				}
				file.transferTo(destFile);
				result = destFileName;
				// doctorService.updateDoctorDiploma(0, result);
			} else {
				result = "false";
			}
		} catch (Exception e) {
			result = "false";
		}
		return result;
	}

	@RequestMapping("/keep")
	public void keep(@RequestParam("doctorCode") String doctorCode) {
		try {
			onlineDoctorService.keepTouch(doctorCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/online")
	@ResponseBody
	public Object online() {
		HashMap<String, Object> object = new HashMap<String, Object>();
		try {

			object.put("onlineDoctorCode", onlineDoctorService.get1OnlineDoctor());

		} catch (Exception e) {
			object.put("onlineDoctorCode", "-1");

			e.printStackTrace();
		}
		return object;
	}

}
